package bestTeam.WalkieDoggie.client.Composites;

import com.google.code.gwteyecandy.Tooltip;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;

import bestTeam.WalkieDoggie.client.WalkieDoggie;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Button;

public class SideComp extends Composite{

	private Label credit_label;
	private Label comment;

	public SideComp(){
		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		
		verticalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		verticalPanel.setSpacing(10);
		verticalPanel.setWidth("150px");
		verticalPanel.setHeight("100%");
		HorizontalPanel h = new HorizontalPanel();
		verticalPanel.add(h);
		

		Image help = new Image("qm.png");
		h.add(help);
		
		Tooltip t1 = new Tooltip();
		t1.setText("Credit is your account balance:\n You lose points when you add a request;\n You earn points when you volunteer to others requests;\n You must have positive balance in order to create a request.");
		t1.attachTo(help);
		
		credit_label = new Label("your credit: "+WalkieDoggie.loginInfo.getUserData().getCredit());
		h.add(credit_label);
		credit_label.setSize("100%", "100%");
		verticalPanel.setCellHorizontalAlignment(h, HasHorizontalAlignment.ALIGN_CENTER);
		
		Button createButton = new Button("New Walkie");
		verticalPanel.add(createButton);
		createButton.setSize("100%", "50px");
		verticalPanel.setCellHorizontalAlignment(createButton, HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setCellVerticalAlignment(createButton, HasVerticalAlignment.ALIGN_TOP);
		
		comment = new Label();
		verticalPanel.add(comment);
			
		Image apk = new Image("app.png");
		verticalPanel.add(apk);
		verticalPanel.setCellHorizontalAlignment(apk, HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setCellVerticalAlignment(apk, HasVerticalAlignment.ALIGN_BOTTOM);
		
		Anchor apkLink = new Anchor("Download Apk");
		apkLink.addClickHandler(new ClickHandler() {
		    @Override
		    public void onClick(ClickEvent event) {
		        Window.open("http://walkie-doggie.googlecode.com/files/WalkieDoggieForAndroid.apk", "download File", "");            
		    }
		});
		
		verticalPanel.add(apkLink);
		verticalPanel.setCellHorizontalAlignment(apkLink, HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setCellVerticalAlignment(apkLink, HasVerticalAlignment.ALIGN_BOTTOM);
		
		
		Image qr = new Image("https://www.google.com/chart?chs=150x150&cht=qr&chl=http://walkie-doggie.googlecode.com/files/WalkieDoggieForAndroid.apk&chld=L|1&choe=UTF-8",0, 0, 150, 150);
		verticalPanel.add(qr);
		verticalPanel.setCellHorizontalAlignment(qr, HasHorizontalAlignment.ALIGN_RIGHT);
		
		WalkieDoggie.create_walkies_comp = new createWalkieComposite();
		WalkieDoggie.create_walkies_comp.setPopupPosition(WalkieDoggie.header_comp.getAbsoluteLeft()+400, WalkieDoggie.header_comp.getAbsoluteTop()+100);

		createButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(WalkieDoggie.loginInfo.isNewUser()){
					WalkieDoggie.mainPanel.clear();
					WalkieDoggie.mainPanel.add(WalkieDoggie.profile_comp);
				}
				else if(WalkieDoggie.loginInfo.getUserData().getCredit()<=0){
					comment.setText("no credit available :(");
				}
				else{
					WalkieDoggie.create_walkies_comp.setGlassEnabled(true);
					WalkieDoggie.create_walkies_comp.show();
					comment.setText("");
				}
					
			}
		});

	}

	public Label getCredit_label() {
		return credit_label;
	}

	public void setCredit_label(Label credit_label) {
		this.credit_label = credit_label;
	}
	
	public void setCredit_text() {
		this.credit_label.setText("your credit: "+WalkieDoggie.loginInfo.getUserData().getCredit());
	}
}